// TypeScript file
class Setting  {

    private speed = .2;
    private context: MainPanel;
    private sound_start;
    private sound_bw;
    private sound_end;
    private group: eui.Group;

    //游戏音乐控制
    private gameSoundOnBtn: eui.ToggleButton;
    private gameSoundOffBtn: eui.ToggleButton;

    //背景音效控制
    private backgroudSoundOnBtn: eui.ToggleButton;
    private backgroudSoundOffBtn: eui.ToggleButton;

    //活动显示控制
    private displayOnBtn: eui.ToggleButton;
    private displayOffBtn: eui.ToggleButton;

    private volumeProgressBtn: eui.Image;
    private volumeProgressBgMask:eui.Image;
    private volumeProgressBg:eui.Image;

    private hSlilder:eui.HSlider;

    private hash;

    private volumeProgressMaxWidth:number = 900;
    private touchStatus:boolean = false;              
    private distance:egret.Point = new egret.Point(); 





    public constructor(context: MainPanel, group: eui.Group) {

        this.context = context;
        this.group = group;

        this.gameSoundOnBtn = < eui.ToggleButton > this.group.getChildByName("gameSound_on");
        this.gameSoundOffBtn = < eui.ToggleButton > this.group.getChildByName("gameSound_off");


        this.backgroudSoundOnBtn = < eui.ToggleButton > this.group.getChildByName("backgroudSound_on");
        this.backgroudSoundOffBtn = < eui.ToggleButton > this.group.getChildByName("backgroudSound_off");


        this.displayOnBtn = < eui.ToggleButton > this.group.getChildByName("display_on");
        this.displayOffBtn = < eui.ToggleButton > this.group.getChildByName("display_off");

        this.volumeProgressBtn = < eui.Image > this.group.getChildByName("volumeProgressBtn");
        this.volumeProgressBtn.touchEnabled = false;
        // this.volumeProgressBtn.alpha = 0;





        this.hash = {
            "gameSound": {
                "on": this.gameSoundOnBtn,
                "off": this.gameSoundOffBtn,
            },
            "backgroudSound": {
                "on": this.backgroudSoundOnBtn,
                "off": this.backgroudSoundOffBtn,
            },
            "display": {
                "on": this.displayOnBtn,
                "off": this.displayOffBtn,
            }
        }




        //音量进度条设置
        var volumeProgressBg = this.volumeProgressBg = new eui.Image();
        volumeProgressBg.source = "yinliangtiao_png";
        volumeProgressBg.x = 580;
        volumeProgressBg.y = 104;
        volumeProgressBg.name = "yinliangtiao";
        this.group.addChild(volumeProgressBg);


        var volumeProgressBgMask = this.volumeProgressBgMask = new eui.Image();
        volumeProgressBgMask.source = "yinliangtiao_png";
        volumeProgressBgMask.x = 580;
        volumeProgressBgMask.y = 104;
        volumeProgressBgMask.width = 100 * this.context.soundctr.volume * (this.volumeProgressMaxWidth/100);
        this.group.addChild(volumeProgressBgMask);
        volumeProgressBg.mask = volumeProgressBgMask;
        this.group.setChildIndex(volumeProgressBg,this.group.getChildIndex(this.volumeProgressBtn)-1);

        this.setVolumeBtn();

        var exml =
            `<e:Skin minWidth="20" minHeight="8"  xmlns:e="http://ns.egret.com/eui">
                <e:Image id="track" source="`+SystemManger.RESURL+`assets/Slider/track.png" scale9Grid="1,1,4,4" width="100%" height="6" verticalCenter="0"/>
                <e:Image id="trackHighlight" source="`+SystemManger.RESURL+`assets/Slider/tracklight.png" scale9Grid="1,1,4,4" height="6" verticalCenter="0"/>
                <e:Image id="thumb" source="`+SystemManger.RESURL+`assets/Slider/thumb.png"  scaleX="5" scaleY="5" verticalCenter="0"/>
            </e:Skin>`;
        /**
         * 音量控制组件调整的组件 
         */
        this.hSlilder = new eui.HSlider();
        this.hSlilder.skinName = exml;
        this.hSlilder.width = 990;
        this.hSlilder.height = 20;
        this.hSlilder.maximum = 100;
        this.hSlilder.value = 100 * this.context.soundctr.volume;
        this.hSlilder.y = 120;
        this.hSlilder.x = 530;
        this.hSlilder.alpha = 0;
        ///监听 CHANGE 事件
        this.group.addChild(this.hSlilder);

        this.initDisplay();
        this.initEvent();




    }

    public onRemove():void{
        this.group.removeChild(this.hSlilder);
        this.group.removeChild(this.volumeProgressBg);
        this.group.removeChild(this.volumeProgressBgMask);

        this.gameSoundOnBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.gameSoundOffBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)

        this.backgroudSoundOnBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.backgroudSoundOffBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)

        this.displayOnBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.displayOffBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)

        this.hSlilder.removeEventListener(egret.Event.CHANGE, this.onHSliderChange, this);
    }

    /**
     * 初始化事件
     */
    private initEvent(): void {

        this.gameSoundOnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.gameSoundOffBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)


        this.backgroudSoundOnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.backgroudSoundOffBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)


        this.displayOnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)
        this.displayOffBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapEvent, this)

        this.hSlilder.addEventListener(egret.Event.CHANGE, this.onHSliderChange, this);
    }

    /**
     * 按钮时间响应
     */
    private tapEvent(event: egret.Event): void {
        var name = event.target.name;
        var splitArray: string[] = name.split("_");
        if (splitArray[1] == "on" || splitArray[1] == "onIcon" ) {
            this.setTapButton(splitArray[0], true);
            this.setTapValue(splitArray[0], true);
        } else {
            this.setTapButton(splitArray[0], false);
            this.setTapValue(splitArray[0], false);
        }

    }

    /**
     * 音量控制界面
     */
    private onHSliderChange(e: egret.Event) {
        var slilder = <eui.HSlider>e.target;
        this.context.soundctr.volumeCtr(slilder.pendingValue / 100);

        this.volumeProgressBgMask.width = 100 * this.context.soundctr.volume * (this.volumeProgressMaxWidth/100);
        this.setVolumeBtn();
    }




    /**
     * 按钮状态切换
     */
    private setTapButton(classic: string, state: boolean): void {
        var object = this.hash[classic];
        object["on"].selected = !state;
        object["off"].selected = state;
    }

    private setTapValue(classic: string, state: boolean) {
        switch (classic) {
            case "gameSound":
                {
                    if (state) {
                        this.context.soundctr.openGameSound();
                    } else {
                        this.context.soundctr.closeGameSound();
                    }
                    break;
                }

            case "backgroudSound":
                {
                    if (state) {
                        this.context.soundctr.playBgm();
                    } else {
                        this.context.soundctr.closeBgm();
                    }
                    break;
                }

            case "display":
                {
                    if (state) {
                        PreferenceData.setPromotionOpen(1);
                    } else {
                        PreferenceData.setPromotionOpen(0);
                    }
                    break;
                }
        }
    }


    private initDisplay(): void {
        if (this.context.soundctr.isGameSound) {
            this.setTapButton("gameSound", true);
        } else {
            this.setTapButton("gameSound", false);
        }


        if (this.context.soundctr.isBgmSound) {
            this.setTapButton("backgroudSound", true);
        } else {
            this.setTapButton("backgroudSound", false);
        }


        if (PreferenceData.getPromotionOpen()) {
            this.setTapButton("display", true);
        } else {
            this.setTapButton("display", false);
        }

    }

    private setVolumeBtn():void
    {
        this.volumeProgressBtn.x = (this.volumeProgressMaxWidth/100) * this.context.soundctr.volume*100 + this.volumeProgressBgMask.x - this.volumeProgressBtn.width/2;
    }





}